package com.wwwarehouse.carddesk.fragment.teamfragment.checkdownclassfragemnt;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.GridView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.adapter.teamadapter.DownClassInfoAdapter;
import com.wwwarehouse.carddesk.bean.CardDeskGroupGetGroupBean;
import com.wwwarehouse.carddesk.constant.CardDeskConstant;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.LogUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建者 chenchao.
 * @创建时间 2017/11/9 19:34
 * @描述 ${TODO}
 */

public class DownClassFragmentChild extends BaseFragment {

    private View mView;
    private StateLayout mStateLayout;
    private GridView mGridView;
    private int mPage;
    private Map<String, Object> mDownClassMap;
    private String mAuthorizerId;
    private Bundle mToCheckDownBundle;
    private String mLeadFaceUrl;
    private String mLeadName;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_down_class_child, container, false);
        initView();
        initData();
        return mView;
    }

    private void initData() {
        mToCheckDownBundle = new Bundle();

        mDownClassMap = new HashMap<>();
        if (getArguments() != null) {
            Bundle bundle = getArguments();
            mPage = bundle.getInt("page");
            mAuthorizerId = bundle.getString("authorizerId");
            mLeadFaceUrl = bundle.getString("leadFaceUrl");
            mLeadName = bundle.getString("leadName");
            mDownClassMap.put("authorizerId", mAuthorizerId);
            mDownClassMap.put("page", mPage);
            mDownClassMap.put("size", 9);
        }
        getDownClass();
    }

    private void getDownClass() {
        mStateLayout.setAllContentViewMargin(ConvertUtils.dip2px(mActivity,11),ConvertUtils.dip2px(mActivity,5),ConvertUtils.dip2px(mActivity,11),ConvertUtils.dip2px(mActivity,0));
        mStateLayout.showProgressView(true);
        NoHttpUtils.httpPost(CardDeskConstant.GET_DOWN_INFO, mDownClassMap, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener=new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            try {

                    switch (what) {
                        case 0:
                            if ("0".equals(commonClass.getCode())) {
                                final CardDeskGroupGetGroupBean cardDeskGroupGetGroupBean = JSON.parseObject(commonClass.getData().toString(), CardDeskGroupGetGroupBean.class);
                                final List<CardDeskGroupGetGroupBean.ListBean> downClassInfoBeanList = cardDeskGroupGetGroupBean.getList();
                                DownClassInfoAdapter downClassInfoAdapter = new DownClassInfoAdapter(mActivity, downClassInfoBeanList);
                                mGridView.setAdapter(downClassInfoAdapter);
                                mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                        CheckDownClassFragment checkDownClassFragment = new CheckDownClassFragment();
                                        mToCheckDownBundle.putParcelable("bean", downClassInfoBeanList.get(position));
                                        mToCheckDownBundle.putString("leadFaceUrl", mLeadFaceUrl);
                                        mToCheckDownBundle.putString("leadName",mLeadName);
                                        mToCheckDownBundle.putString("userId", mAuthorizerId);
                                        checkDownClassFragment.setArguments(mToCheckDownBundle);
                                        pushFragment(checkDownClassFragment);
                                    }
                                });
                            } else {
                                mStateLayout.showSystemView(commonClass.getMsg(), false);
                                mStateLayout.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        getDownClass();
                                    }
                                });
                            }
                            break;
                        default:
                            break;

                }

            } catch (Exception e) {
                LogUtils.showLog(e.toString());
            }
        }

        @Override
        public void onFailed(String result, int what) {
            mStateLayout.showNetworkView(false);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getDownClass();
                }
            });
        }

        @Override
        public void onFinish(int what) {
            mStateLayout.showContentView();
        }
    };
    private void initView() {
        mStateLayout = findView(mView, R.id.sl_layout);
        mGridView = findView(mView, R.id.gv);
    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }
}
